package com.eian.boot.crypto.enums;

import lombok.Getter;

/**
 * 加密算法枚举
 *
 * @author eian
 */
@Getter
public enum CryptoAlgorithm {

    /**
     * 对称加密算法
     */
    AES_CBC("AES/CBC/PKCS5Padding", CryptoType.SYMMETRIC, "AES-CBC模式"),
    AES_GCM("AES/GCM/NoPadding", CryptoType.SYMMETRIC, "AES-GCM模式"),
    SM4_CBC("SM4/CBC/PKCS5Padding", CryptoType.SYMMETRIC, "SM4-CBC模式（国密）"),
    SM4_ECB("SM4/ECB/PKCS5Padding", CryptoType.SYMMETRIC, "SM4-ECB模式（国密）"),

    /**
     * 非对称加密算法
     */
    RSA("RSA/ECB/PKCS1Padding", CryptoType.ASYMMETRIC, "RSA加密"),
    RSA_OAEP("RSA/ECB/OAEPPadding", CryptoType.ASYMMETRIC, "RSA-OAEP加密"),
    SM2("SM2", CryptoType.ASYMMETRIC, "SM2加密（国密）");

    /**
     * 算法转换字符串（transformation）
     */
    private final String transformation;

    /**
     * 加密类型
     */
    private final CryptoType type;

    /**
     * 描述
     */
    private final String description;

    CryptoAlgorithm(String transformation, CryptoType type, String description) {
        this.transformation = transformation;
        this.type = type;
        this.description = description;
    }

    /**
     * 获取算法名称（不含模式和填充）
     */
    public String getAlgorithmName() {
        return transformation.split("/")[0];
    }

    /**
     * 是否是国密算法
     */
    public boolean isGM() {
        return this == SM2 || this == SM4_CBC || this == SM4_ECB;
    }

    /**
     * 是否是对称加密
     */
    public boolean isSymmetric() {
        return type == CryptoType.SYMMETRIC;
    }

    /**
     * 是否是非对称加密
     */
    public boolean isAsymmetric() {
        return type == CryptoType.ASYMMETRIC;
    }
}

